package com.bw.a1908.ui.main;

import android.graphics.Color;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bw.a1908.R;
import com.bw.a1908.base.StatusActivity;
import com.bw.a1908.core.mvp.BaseActivity;
import com.bw.a1908.core.mvp.IPresenter;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主页面
 */
public class MainActivity extends StatusActivity<IPresenter> implements BottomBarLayout.OnItemSelectedListener {

    private BottomBarLayout bottomBarLayout;
    //构建对应页面Class
    private List<Class<? extends Fragment>> fragmentList = new ArrayList<>();
    //页面缓存
    private Map<Integer, Fragment> fragmentMap = new HashMap<>();

    @Override
    public void initBar() {
        statusTransBar();
    }

    @Override
    public int bandLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        bottomBarLayout = findViewById(R.id.act_main_bottom_layout);
        bottomBarLayout.setOnItemSelectedListener(this);

        //修改状态栏颜色
//        BarUtils.setStatusBarColor(this, Color.RED);
        //修改状态栏图标颜色
//        BarUtils.setStatusBarLightMode(this, true);

        BarUtils.transparentStatusBar(this);
    }

    @Override
    public void initData() {
        fragmentList.add(MainFragment.class);
        fragmentList.add(MineFragment.class);
        fragmentList.add(DefFragment.class);
        fragmentList.add(DefFragment.class);
        fragmentList.add(DefFragment.class);

        bottomBarLayout.setCurrentItem(0);
    }

    //FragmentManager 管理页码 add show hide replace
    private int myPreviousPosition = -1;

    @Override
    public void onItemSelected(BottomBarItem bottomBarItem, int previousPosition, int currentPosition) {
        //展示页面 currentPosition
        //页面构建过没有？ ->  fragmentMap 中有没有
        if (!fragmentMap.containsKey(currentPosition)) {
            //fragmentMap 没有
            try {
                fragmentMap.put(currentPosition, fragmentList.get(currentPosition).newInstance());
            } catch (IllegalAccessException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        //页面构建完成 -> 展示
        //页面之前展示没有？ add show
        if (fragmentMap.get(currentPosition).isAdded()) {
            //show 之前容器页面问题？
            if (myPreviousPosition > -1) {
                getSupportFragmentManager().beginTransaction().show(fragmentMap.get(currentPosition)).hide(fragmentMap.get(myPreviousPosition)).commit();
            } else {
                getSupportFragmentManager().beginTransaction().show(fragmentMap.get(currentPosition)).commit();
            }
        } else {
            //add 之前容器页面问题？
            if (myPreviousPosition > -1) {
                //之前有展示的页面
                getSupportFragmentManager().beginTransaction().add(R.id.act_main_layout, fragmentMap.get(currentPosition)).hide(fragmentMap.get(myPreviousPosition)).commit();
            } else {
                LogUtils.d("currentPosition-" + currentPosition);
                getSupportFragmentManager().beginTransaction().add(R.id.act_main_layout, fragmentMap.get(currentPosition)).commit();
            }
        }
        myPreviousPosition = currentPosition;
    }


}